﻿using AutoMapper;
using IMES.Common.Domain.DariusModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Darius.API.Application.Commands
{
    public class PermissionSearchHandler : IRequestHandler<PermissionSearchCommand, Result>
    {
        private readonly IBaseRepositorys<Permission> permissionRepository;
        private readonly ILogger<PermissionSearchHandler> logger;

        public PermissionSearchHandler(IBaseRepositorys<Permission> permissionRepository, ILogger<PermissionSearchHandler> logger)
        {
            this.permissionRepository = permissionRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 权限查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PermissionSearchCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result() { Code = (int)CodeEnum.search };
                var list = permissionRepository.GetAll().ToList();
                result.Data = list;
                logger.LogInformation("权限查询成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "权限查询失败");
                return new Result() { Code = (int)CodeEnum.error, Messege = "权限查询失败" };
            }
        }
    }
}
